package com.springbootblog.controller.fontend;

import com.springbootblog.service.fontend.LoginService;
import com.springbootblog.utils.Email;
import com.springbootblog.utils.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录部分接口
 * @className AboutController
 * @author camellia
 * @email 805795955@qq.com
 * @date 2024/01/01
 * @version 1.0
 */
@RequestMapping("java")
@RestController
//@CrossOrigin(origins="*",maxAge=3600)
public class LoginController
{
    /**
     * 自动装载（service接口注入）
     */
    @Autowired
    private LoginService loginService;

    @GetMapping("login/accountLogin")
    public Map<String,Object> accountLogin(@RequestParam(defaultValue = "") String params, HttpServletRequest request) throws Exception
    {
        if(params == null || params.equals("") || params.isEmpty())
        {
            Map<String,Object> result = new HashMap<>(2);
            result.put("code",-1);
            result.put("msg","参数错误！");
            return result;
        }
        return loginService.accountLogin(params,request);
    }

    /**
     * 退出登录
     * @param request
     * @return
     * @throws Exception
     */
    @GetMapping("login/layout")
    public Map<String,Object> layout(HttpServletRequest request) throws Exception
    {
        // 获取session对象
        HttpSession session = request.getSession();
        // 清空session
        session.invalidate();

        Map<String,Object> result = new HashMap<>(2);
        result.put("code",1);
        result.put("msg","操作成功！");
        return result;
    }

    /**
     * 发邮件
     * @param params
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("login/sendEmailCode")
    public Map<String,Object> sendEmailCode(@RequestBody String params, HttpServletRequest request) throws Exception {
        if(params == null || params.isEmpty())
        {
            Map<String,Object> result = new HashMap<>(3);
            result.put("code",-1);
            result.put("msg","参数错误！");
            result.put("params",params);
            return result;
        }
        return loginService.sendEmailCode(params,request);
    }

    /**
     * 修改用户昵称
     * @param params
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("login/updateNickname")
    public Map<String,Object> updateNickname(@RequestBody String params, HttpServletRequest request) throws Exception
    {
        if(params == null || params.isEmpty())
        {
            Map<String,Object> result = new HashMap<>(3);
            result.put("code",-1);
            result.put("msg","参数错误！");
            result.put("params",params);
            return result;
        }
        return loginService.updateNickname(params,request);
    }







}
